{extend name="common/common" /}

{block name="style"}
<style media="screen">
	.treeBox {
		height: 400px;
		overflow: auto;
	}
</style>
{/block}

{block name="content"}
<el-card class="box-card">
	<div slot="header">
		<span class="span-primary"><i class="fa fa-user"></i></span>
		角色列表
		{if authCheck('role/roleadd')}
			<el-button type="primary" plain size="small" class="header-btn" @click="addRow">添加角色</el-button>
		{/if}
	</div>
	<div id="table-box">
		<el-form :inline="true" class="search-table-form">
			<el-form-item>
				<el-input
					placeholder="请输入搜索内容"
					clearable="true"
					prefix-icon="el-icon-search"
					v-model="searchText">
				</el-input>
			</el-form-item>
			<el-form-item>
				<el-button type="info" plain @click="getTable">查询</el-button>
			</el-form-item>
		</el-form>
		<el-table :data="tableData" stripe="true">
			<el-table-column align="center" prop="id" label="ID" width="50"></el-table-column>
			<el-table-column align="center" prop="role_name" label="角色名称" width="200"></el-table-column>
			<el-table-column align="center" prop="operate" label="权限">
				<template slot-scope="scope">
					{if authCheck('role/giveaccess')}
					<el-button
						@click="giveAccess(scope.row)"
						size="small" plain type="primary"
						icon="fa fa-magic"> 操作权限
					</el-button>
					{/if}
				</template>
			</el-table-column>
			<el-table-column prop="operate" label="操作">
				<template slot-scope="scope">
					{if authCheck('role/roleedit')}
					<el-button
						@click="editRow(scope.row)"
						size="small" plain type="success"
						icon="el-icon-edit">
					</el-button>
					{/if}
					{if authCheck('role/roledel')}
					<el-button
						@click="delRow(scope.row)"
						size="small" plain 	type="danger"
						icon="el-icon-delete">
					</el-button>
					{/if}
				</template>
			</el-table-column>
		</el-table>
		<div class="table-page">
			<el-pagination
				@size-change="pageSizeChange"
				@current-change="curPageChange"
				:current-page="pageNumber"
				:page-sizes="[10, 20, 50, 75]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="tableTotal">
			</el-pagination>
		</div>
	</div>
</el-card>
{/block}

{block name="script"}
<script type="text/javascript">
var main = new Vue({
	el:"#main",
	data:function(){
		return {
			loading:true,
			tableData:'',
			searchText:'',
			pageSize:10,
			pageNumber:1,
			tableTotal:0,
			editAccessID:0,
		}
	},
	mounted:function(){
		this.getTable();
	},
	methods:{
		getTable(){
			this.loading=true;
			$.post('./index',{
				'pageSize':this.pageSize,
				'pageNumber':this.pageNumber,
				'searchText':this.searchText
			},function(res){
				main.loading=false;
				if(res.code == 1){
					main.$message({message:res.msg,type:'success'});
					main.tableData = res.data.rows;
					main.tableTotal = res.data.total;
				}else{
					main.$notify.error({title:'失败',message:res.msg});
				}
			})
		},
		pageSizeChange(val) {
			this.pageSize = val;
			this.getTable();
		},
		curPageChange(val) {
			this.pageNumber = val;
			this.getTable();
		},
		addRow() {
			FormBox.subType = "add";
			FormBox.title = "添加角色";
			FormBox.is_show = true;
		},
		editRow(row){
			if(row.id == 1){
				main.$notify.error({title: '失败',message: '不可编辑超级管理员角色',});
			}else{
				FormBox.subType = row.id;
				FormBox.title = "编辑 “" + row.role_name + "” 角色";
				FormBox.is_show = true;
			}
		},
		delRow(row){
			if(row.id == 1){
				main.$notify.error({title: '失败',message: '不可删除超级管理员角色',});
				return false;
			}
			this.$confirm('此操作将永久删除该数据,是否继续?','提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			}).then((res) => {
				$.post("{:url('role/roleDel')}",{"id":row.id},function(res){
					if(res.code == 1){
						main.$notify({title:"成功",type: 'success',message: res.msg});
						main.getTable()
					}else{
						main.$error({title: '错误'+res.code,message: res.msg});
					}
				})
			}).catch((res) => {
				this.$notify.info({title:"消息",message: "已取消删除"})
			});
		},
		giveAccess(row) {
			this.editAccessID = row.id;
			if(row.id == 1){
				main.$notify.error({title:'失败',message:'不可修改超级管理员角色'});
				return false;
			}
			nodeTree.showNodeTree = true;
			$.getJSON("{:url('role/giveAccess')}",{'type':'get','id':row.id},function(res){
				nodeTree.nodeTree = res.data.node;
				var arr = [];
				for(i in res.data.rule){
					arr.push(res.data.rule[i]);
				}
				nodeTree.rules = arr;
			});
		}
	}
})
</script>
{/block}

{block name="component"}
<div id="nodeTreeBox">
	<el-dialog title="操作权限" :visible.sync="showNodeTree" top="5vh">
		<div class="treeBox">
			<el-tree
				:data="nodeTree"
				ref="nodeTree"
				show-checkbox
				node-key="id"
				default-expand-all = "true"
				:default-checked-keys="rules">
			</el-tree>
		</div>
		<div slot="footer" class="dialog-footer">
			<el-button @click="showNodeTree = false">取消</el-button>
			<el-button type="success" @click="saveAccess">保存</el-button>
		</div>
	</el-dialog>
</div>
<script type="text/javascript">
var nodeTree = new Vue({
	el:"#nodeTreeBox",
	data() {
		return {
			showNodeTree:false,
			nodeTree:'',
			rules:[]
		}
	},
	methods:{
		saveAccess() {
			var AccessArr = this.$refs.nodeTree.getCheckedKeys();
			var HalfArr = this.$refs.nodeTree.getHalfCheckedKeys();
			for(i in HalfArr){
				AccessArr.push(HalfArr[i]);
			}
			$.post("./giveAccess",{
				'type':'give',
				'id':main.editAccessID,
				'rule':AccessArr
			},function(res){
				if(res.code == 1){
					nodeTree.showNodeTree = false;
					main.$notify({title:"成功",message: res.msg,type: 'success'});
				}else{
					main.$notify.error({title: '失败',message: res.msg});
				}
			})
		}
	}
})
</script>

<div id="FormBox">
	<el-dialog :title="title" :visible.sync="is_show" top="5vh">
		<div class="el-dialog-ct">
			<el-form label-width="80px" :model="form" :rules="rules" ref="form" inline>
				<el-form-item label="角色名称" prop="role_name">
					<el-input v-model="form.role_name" required clearable></el-input>
				</el-form-item><br>
			</el-form>
		</div>
		<div slot="footer" class="dialog-footer">
			<el-button @click="is_show = false">取消</el-button>
			<el-button type="success" @click="subForm">保存</el-button>
		</div>
	</el-dialog>
</div>
<script type="text/javascript">
var FormBox = new Vue({
	el:"#FormBox",
	data:{
		subType:"add",
		is_show:false,
		title:"添加角色",
		form:{
			role_name:""
		},
		rules:{
			role_name:[{required:true,message:'请输入角色名称',trigger:['blur','change']}],
		}
	},
	methods:{
		subForm() {
			if(this.subType == "add"){
				var param = this.form;
				var url = '{:url("Role/roleAdd")}';
			}else{
				var param = {
					role_name:this.form.role_name,
					id:this.subType
				};
				var url = '{:url("Role/roleEdit")}';
			}
			this.$refs['form'].validate((valid) => {
				if(valid){
					$.post(url,param,function(res) {
						if(res.code == 1){
							main.$notify({title:"成功",type:"success",message:res.msg});
							main.getTable();
							FormBox.is_show = false;
						}else{
							main.$notify.error({title:"错误",message:res.msg});
						}
					});
				}
			});
		}
	}
});
</script>
{/block}
